class_name ExtAttr
extends Reference

var query: QueryManager
var hp_m: Multi
var data_m: Multi

const QUERY_LIST = [["hp_m", "HpC"], ["data_m", "DataC"]]
const REGISTER_FUNCS = ["add_hp", "add_hp_max"]


func _init(world: EcsWorld):
	query = world.query_manager
	var _c = world.connect("world_ready", self, "on_world_ready")

	for func_name in REGISTER_FUNCS:
		var ref = funcref(self, func_name)
		Funcs.reg(func_name, ref)


func on_world_ready():
	for item in QUERY_LIST:
		var multi = query.fetch_data(item[1])
		set(item[0], multi)


func add_hp(buff: Buff, target: int, type: String, value, _bt):
	var hp = hp_m.get(target) as HpC
	var data = data_m.get(target) as DataC
	if data.dead:
		return
	match type:
		"maxHp":
			value = value * hp.max_hp
		"curHp":
			value = value * hp.value
	value = floor(value)
	hp.add_hp(value)
	print("%d add hp:" % buff.id, value)


func add_hp_max(_buff: Buff, target: int, _type: String, value, _bt):
	var hp = hp_m.get(target) as HpC
	var old_max = hp.max_hp
	hp.max_hp = floor(old_max * (1 + value))
	hp.add_hp(hp.max_hp - old_max)
